﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web.Services;
using roncliProductions.LibWowAPI;
using roncliProductions.LibWowAPI.Character;
using roncliProductions.LibWowAPI.Enums;
using roncliProductions.LibWowAPI.Internationalization;
using Profession = roncliProductions.LibWowAPI.Character.Profession;

namespace FrontEnd.webservice
{
    /// <summary>
    /// Summary description for DataCollector
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
    public class DataCollector : WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string InsertCharacter(string connectionString, string characterId, string realmId, string characterJSon)
        {
            try
            {
                var characterProfile = new CharacterProfile();
                characterProfile.Clear();
                characterProfile.Data = characterJSon;
                characterProfile.Load();
                Character character = characterProfile.Character;
                string sideId = (character.Race.Side == Side.Alliance ? "1" : "2");

                InsertCharacterAchievements(character.Achievements, characterId, connectionString);
                InsertCharacterAppearance(character.Appearance, characterId, connectionString);
                InsertCharacterCompanions(character.Companions, characterId, connectionString);
                InsertCharacterEquipment(character.Items, characterId, connectionString);
                InsertCharacterMounts(character.Mounts, characterId, connectionString);
                InsertCharacterPets(character.Pets, characterId, connectionString);
                InsertCharacterProfessions(character.Professions, characterId, connectionString);
                if (character.Level > 55)
                {
                    InsertCharacterProgression(character.Progression, characterId, connectionString);
                }
                InsertCharacterPvp(character.PvP, characterId, realmId, connectionString);
                InsertCharacterQuests(character.Quests, characterId, connectionString);
                InsertCharacterReputation(character.Reputation, characterId, connectionString);
                InsertCharacterStats(character.Stats, characterId, connectionString);
                InsertCharacterTalent(character.Talents, characterId, connectionString);
                InsertCharacterTitles(character.Titles, characterId, connectionString);
                string guildId = InsertCharacterGuild(character.Guild, realmId, sideId, connectionString);
                InsertCharacterBaseData(character, guildId, sideId, realmId, character.PvP, connectionString);

                return "done";
            }
            catch (Exception exception)
            {
                return exception.Message + Environment.NewLine + exception.StackTrace;
            }
        }

        private void InsertCharacterPvp(PvP pvp, string characterId, string realmId, string connectionString)
        {
            if (pvp != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                foreach (Battleground battleground in pvp.RatedBattlegrounds.Battlegrounds)
                {
                    var sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacterBattleground"
                                         };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                    sqlCommand.Parameters.AddWithValue("BattlegroundName", battleground.Name);
                    sqlCommand.Parameters.AddWithValue("Played", battleground.Played);
                    sqlCommand.Parameters.AddWithValue("Won", battleground.Won);
                    sqlCommand.ExecuteScalar();
                }

                var sqlDeleteCommand = new SqlCommand
                                           {
                                               Connection = sqlConnection
                                           };
                sqlDeleteCommand.CommandType = CommandType.Text;
                sqlDeleteCommand.CommandText = "DELETE FROM Character_ArenaTeams WHERE CharacterId='" + characterId + "'";
                sqlDeleteCommand.ExecuteNonQuery();

                foreach (ArenaTeam arenaTeam in pvp.ArenaTeams)
                {
                    var sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertArenaTeam"
                                         };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("Name", arenaTeam.Name);
                    sqlCommand.Parameters.AddWithValue("Size", arenaTeam.Size);
                    sqlCommand.Parameters.AddWithValue("Rating", arenaTeam.TeamRating);
                    sqlCommand.Parameters.AddWithValue("RealmId", realmId);
                    string arenaId = sqlCommand.ExecuteScalar().ToString();

                    sqlCommand.CommandText = "InsertCharacter_ArenaTeam";
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                    sqlCommand.Parameters.AddWithValue("ArenaId", arenaId);
                    sqlCommand.Parameters.AddWithValue("Rating", arenaTeam.PersonalRating);
                    sqlCommand.ExecuteScalar();
                }
                sqlConnection.Close();
            }
        }

        private void InsertCharacterQuests(IEnumerable<int> quests, string characterId, string connectionString)
        {
            if (quests != null)
            {
                string completedQuests = ConvertListToString(quests);

                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacterQuests"
                                     };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("QuestsCompleted", completedQuests);
                sqlCommand.ExecuteScalar();

                sqlConnection.Close();
            }
        }

        private void InsertCharacterBaseData(Character character, string guildId, string sideId, string realmId, PvP pvp, string connectionString)
        {
            if (character != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacter"
                                     };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("Name", character.Name);
                sqlCommand.Parameters.AddWithValue("Level", character.Level);
                sqlCommand.Parameters.AddWithValue("Class", character.Class.ID);
                sqlCommand.Parameters.AddWithValue("Race", character.Race.ID);
                sqlCommand.Parameters.AddWithValue("Gender", character.Gender);
                sqlCommand.Parameters.AddWithValue("AchievementPoints", character.AchievementPoints);
                sqlCommand.Parameters.AddWithValue("Thumbnail", character.Thumbnail);
                sqlCommand.Parameters.AddWithValue("TotalHonorableKills", (pvp != null ? pvp.TotalHonorableKills : 0));
                sqlCommand.Parameters.AddWithValue("PersonalBattlegroundRating", (pvp != null && pvp.RatedBattlegrounds != null ? pvp.RatedBattlegrounds.PersonalRating : 0));
                if (string.IsNullOrEmpty(guildId))
                {
                    sqlCommand.Parameters.AddWithValue("GuildId", DBNull.Value);
                }
                else
                {
                    sqlCommand.Parameters.AddWithValue("GuildId", guildId);
                }
                sqlCommand.Parameters.AddWithValue("RealmId", realmId);
                sqlCommand.Parameters.AddWithValue("SideId", sideId);
                sqlCommand.Parameters.AddWithValue("LastModified", character.LastModified);
                sqlCommand.ExecuteScalar();

                sqlConnection.Close();
            }
        }

        private string InsertCharacterGuild(Guild guild, string realmId, string side, string connectionString)
        {
            string guildId = "";
            if (guild != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = "InsertGuild";
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("Name", guild.Name);
                sqlCommand.Parameters.AddWithValue("RealmId", realmId);
                sqlCommand.Parameters.AddWithValue("Level", guild.Level);
                sqlCommand.Parameters.AddWithValue("Members", guild.Members);
                sqlCommand.Parameters.AddWithValue("Side", side);
                sqlCommand.Parameters.AddWithValue("AchievementPoints", guild.AchievementPoints);
                guildId = sqlCommand.ExecuteScalar().ToString();

                sqlConnection.Close();
            }
            return guildId;
        }

        private void InsertCharacterTitles(IEnumerable<Title> titles, string characterId, string connectionString)
        {
            if (titles != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandText = "DELETE FROM Character_Title WHERE CharacterId='" + characterId + "'";
                sqlCommand.ExecuteNonQuery();

                foreach (Title title in titles)
                {
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandText = "InsertTitle";
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("TitleId", title.ID);
                    sqlCommand.Parameters.AddWithValue("Name", title.Name);
                    sqlCommand.ExecuteScalar();

                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandText = "InsertCharacter_Title";
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                    sqlCommand.Parameters.AddWithValue("TitleId", title.ID);
                    sqlCommand.Parameters.AddWithValue("Selected", title.Selected);
                    sqlCommand.ExecuteScalar();
                }
                sqlConnection.Close();
            }
        }

        private void InsertCharacterTalent(IEnumerable<Talent> talents, string characterId, string connectionString)
        {
            if (talents != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandText = "DELETE FROM CharacterTalent WHERE CharacterId='" + characterId + "'";
                sqlCommand.ExecuteNonQuery();

                foreach (Talent talent in talents)
                {
                    string primeGlyph = "";
                    string majorGlyph = "";
                    string minorGlyph = "";

                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandText = "InsertGlyph";
                    foreach (Glyph glyph in talent.Glyphs.Prime)
                    {
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("GlyphId", glyph.Glyph);
                        sqlCommand.Parameters.AddWithValue("ItemId", glyph.Item);
                        sqlCommand.Parameters.AddWithValue("Icon", glyph.Icon);
                        sqlCommand.Parameters.AddWithValue("Name", glyph.Name);
                        sqlCommand.ExecuteScalar();

                        primeGlyph += glyph.Glyph + ",";
                    }
                    foreach (Glyph glyph in talent.Glyphs.Major)
                    {
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("GlyphId", glyph.Glyph);
                        sqlCommand.Parameters.AddWithValue("ItemId", glyph.Item);
                        sqlCommand.Parameters.AddWithValue("Icon", glyph.Icon);
                        sqlCommand.Parameters.AddWithValue("Name", glyph.Name);
                        sqlCommand.ExecuteScalar();

                        majorGlyph += glyph.Glyph + ",";
                    }
                    foreach (Glyph glyph in talent.Glyphs.Minor)
                    {
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("GlyphId", glyph.Glyph);
                        sqlCommand.Parameters.AddWithValue("ItemId", glyph.Item);
                        sqlCommand.Parameters.AddWithValue("Icon", glyph.Icon);
                        sqlCommand.Parameters.AddWithValue("Name", glyph.Name);
                        sqlCommand.ExecuteScalar();

                        minorGlyph += glyph.Glyph + ",";
                    }

                    sqlCommand.CommandText = "InsertCharacterTalent";
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                    sqlCommand.Parameters.AddWithValue("Build", talent.Build);
                    sqlCommand.Parameters.AddWithValue("Icon", (talent.Icon ?? ""));
                    sqlCommand.Parameters.AddWithValue("Name", (talent.Name ?? ""));
                    sqlCommand.Parameters.AddWithValue("Selected", talent.Selected);
                    sqlCommand.Parameters.AddWithValue("PrimeGlyph", primeGlyph);
                    sqlCommand.Parameters.AddWithValue("MajorGlyph", majorGlyph);
                    sqlCommand.Parameters.AddWithValue("MinorGlyph", minorGlyph);
                    sqlCommand.ExecuteScalar();
                }
                sqlConnection.Close();
            }
        }

        private void InsertCharacterStats(Stats stats, string characterId, string connectionString)
        {
            if (stats != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand();
                sqlCommand.Connection = sqlConnection;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = "InsertCharacterStats";
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("Health", stats.Health);
                sqlCommand.Parameters.AddWithValue("PowerType", stats.PowerType);
                sqlCommand.Parameters.AddWithValue("Power", stats.Power);
                sqlCommand.Parameters.AddWithValue("Strength", stats.Str);
                sqlCommand.Parameters.AddWithValue("Agility", stats.Agi);
                sqlCommand.Parameters.AddWithValue("Intellect", stats.Int);
                sqlCommand.Parameters.AddWithValue("Spirit", stats.Spr);
                sqlCommand.Parameters.AddWithValue("AttackPower", stats.AttackPower);
                sqlCommand.Parameters.AddWithValue("RangedAttackPower", stats.RangedAttackPower);
                sqlCommand.Parameters.AddWithValue("Mastery", stats.Mastery);
                sqlCommand.Parameters.AddWithValue("MasteryRating", stats.MasteryRating);
                sqlCommand.Parameters.AddWithValue("CriticalPct", stats.Crit);
                sqlCommand.Parameters.AddWithValue("CriticalRating", stats.CritRating);
                sqlCommand.Parameters.AddWithValue("HitPct", stats.HitPercent);
                sqlCommand.Parameters.AddWithValue("HitRating", stats.HitRating);
                sqlCommand.Parameters.AddWithValue("HasteRating", stats.HasteRating);
                sqlCommand.Parameters.AddWithValue("ExpertiseRating", stats.ExpertiseRating);
                sqlCommand.Parameters.AddWithValue("SpellPower", stats.SpellPower);
                sqlCommand.Parameters.AddWithValue("SpellPenetration", stats.SpellPen);
                sqlCommand.Parameters.AddWithValue("SpellCriticalPct", stats.SpellCrit);
                sqlCommand.Parameters.AddWithValue("SpellCriticalRating", stats.SpellCritRating);
                sqlCommand.Parameters.AddWithValue("SpellHitPct", stats.SpellHitPercent);
                sqlCommand.Parameters.AddWithValue("SpellHitRating", stats.SpellHitRating);
                sqlCommand.Parameters.AddWithValue("MP5", stats.Mana5);
                sqlCommand.Parameters.AddWithValue("MP5Combat", stats.Mana5Combat);
                sqlCommand.Parameters.AddWithValue("Armor", stats.Armor);
                sqlCommand.Parameters.AddWithValue("DodgePct", stats.Dodge);
                sqlCommand.Parameters.AddWithValue("DodgeRating", stats.DodgeRating);
                sqlCommand.Parameters.AddWithValue("ParryPct", stats.Parry);
                sqlCommand.Parameters.AddWithValue("ParryRating", stats.ParryRating);
                sqlCommand.Parameters.AddWithValue("BlockPct", stats.Block);
                sqlCommand.Parameters.AddWithValue("BlockRating", stats.BlockRating);
                sqlCommand.Parameters.AddWithValue("Resillience", stats.Resil);
                sqlCommand.Parameters.AddWithValue("MainHandDmgMin", stats.MainHandDmgMin);
                sqlCommand.Parameters.AddWithValue("MainHandDmgMax", stats.MainHandDmgMax);
                sqlCommand.Parameters.AddWithValue("MainHandSpeed", stats.MainHandSpeed);
                sqlCommand.Parameters.AddWithValue("MainHandDps", stats.MainHandDps);
                sqlCommand.Parameters.AddWithValue("MainHandExpertise", stats.MainHandExpertise);
                sqlCommand.Parameters.AddWithValue("OffHandDmgMin", stats.OffHandDmgMin);
                sqlCommand.Parameters.AddWithValue("OffHandDmgMax", stats.OffHandDmgMax);
                sqlCommand.Parameters.AddWithValue("OffHandSpeed", stats.OffHandSpeed);
                sqlCommand.Parameters.AddWithValue("OffHandDps", stats.OffHandDps);
                sqlCommand.Parameters.AddWithValue("OffHandExpertise", stats.OffHandExpertise);
                sqlCommand.Parameters.AddWithValue("RangedDmgMin", stats.RangedDmgMin);
                sqlCommand.Parameters.AddWithValue("RangedDmgMax", stats.RangedDmgMax);
                sqlCommand.Parameters.AddWithValue("RangedSpeed", stats.RangedSpeed);
                sqlCommand.Parameters.AddWithValue("RangedDps", stats.RangedDps);
                sqlCommand.Parameters.AddWithValue("RangedCritPct", stats.RangedCrit);
                sqlCommand.Parameters.AddWithValue("RangedCritRating", stats.RangedCritRating);
                sqlCommand.Parameters.AddWithValue("RangedHitPct", stats.RangedHitPercent);
                sqlCommand.Parameters.AddWithValue("RangedHitRating", stats.RangedHitRating);
                sqlCommand.Parameters.AddWithValue("Stamina", stats.Sta);

                sqlCommand.ExecuteScalar();
                sqlConnection.Close();
            }
        }

        private void InsertCharacterReputation(IEnumerable<Reputation> reputations, string characterId, string connectionString)
        {
            if (reputations != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                foreach (Reputation reputation in reputations)
                {
                    if (reputation.Standing > 0 || (reputation.Standing == 0 && reputation.Value > 0))
                    {
                        var sqlCommand = new SqlCommand();
                        sqlCommand.Connection = sqlConnection;
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.CommandText = "InsertFaction";
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("FactionId", reputation.ID);
                        sqlCommand.Parameters.AddWithValue("Name", reputation.Name);

                        sqlCommand.ExecuteScalar();

                        sqlCommand = new SqlCommand();
                        sqlCommand.Connection = sqlConnection;
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.CommandText = "InsertCharacter_Reputation";
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("FactionId", reputation.ID);
                        sqlCommand.Parameters.AddWithValue("Standing", reputation.Standing);
                        sqlCommand.Parameters.AddWithValue("Value", reputation.Value);
                        sqlCommand.Parameters.AddWithValue("Max", reputation.Max);

                        sqlCommand.ExecuteScalar();
                    }
                }
                sqlConnection.Close();
            }
        }

        private void InsertCharacterProgression(Progression progression, string characterId, string connectionString)
        {
            if (progression != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                foreach (Instance instance in progression.Raids)
                {
                    var sqlCommand = new SqlCommand();
                    sqlCommand.Connection = sqlConnection;
                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandText = "InsertRaidInstance";
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("InstanceId", instance.ID);
                    sqlCommand.Parameters.AddWithValue("Name", instance.Name);

                    sqlCommand.ExecuteScalar();

                    foreach (Boss boss in instance.Bosses)
                    {
                        if (boss.NormalKills > 0 || boss.HeroicKills > 0)
                        {
                            sqlCommand = new SqlCommand();
                            sqlCommand.Connection = sqlConnection;
                            sqlCommand.CommandType = CommandType.StoredProcedure;
                            sqlCommand.CommandText = "InsertRaidBoss";
                            sqlCommand.Parameters.Clear();
                            sqlCommand.Parameters.AddWithValue("RaidBossId", boss.ID);
                            sqlCommand.Parameters.AddWithValue("Name", boss.Name);
                            sqlCommand.Parameters.AddWithValue("InstanceId", instance.ID);

                            sqlCommand.ExecuteScalar();

                            sqlCommand = new SqlCommand();
                            sqlCommand.Connection = sqlConnection;
                            sqlCommand.CommandType = CommandType.StoredProcedure;
                            sqlCommand.CommandText = "InsertCharacter_Progression";
                            sqlCommand.Parameters.Clear();
                            sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                            sqlCommand.Parameters.AddWithValue("BossId", boss.ID);
                            sqlCommand.Parameters.AddWithValue("NormalKills", boss.NormalKills);
                            sqlCommand.Parameters.AddWithValue("HeroicKills", boss.HeroicKills);

                            sqlCommand.ExecuteScalar();
                        }
                    }
                }
                sqlConnection.Close();
            }
        }

        private void InsertCharacterProfessions(Professions professions, string characterId, string connectionString)
        {
            if (professions != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         Connection = sqlConnection,
                                         CommandText = "DELETE FROM Character_Profession WHERE CharacterId='" + characterId + "'"
                                     };
                sqlCommand.ExecuteNonQuery();

                if (professions.Primary != null)
                {
                    foreach (Profession profession in professions.Primary)
                    {
                        string rank = profession.Rank.ToString();
                        string max = profession.Max.ToString();
                        string recipies = ConvertListToString(profession.Recipes);
                        string name = profession.Profession.ToString();

                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.CommandText = "InsertCharacter_Profession";
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("Profession", name);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Rank", rank);
                        sqlCommand.Parameters.AddWithValue("Max", max);
                        sqlCommand.Parameters.AddWithValue("Recipes", recipies);

                        sqlCommand.ExecuteScalar();
                    }

                    foreach (Profession profession in professions.Secondary)
                    {
                        string rank = profession.Rank.ToString();
                        string max = profession.Max.ToString();
                        string recipies = ConvertListToString(profession.Recipes);
                        string name = profession.Profession.ToString();

                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.CommandText = "InsertCharacter_Profession";
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("Profession", name);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Rank", rank);
                        sqlCommand.Parameters.AddWithValue("Max", max);
                        sqlCommand.Parameters.AddWithValue("Recipes", recipies);

                        sqlCommand.ExecuteScalar();
                    }
                }
                sqlConnection.Close();
            }
        }

        private void InsertCharacterMounts(IEnumerable<int> mounts, string characterId, string connectionString)
        {
            if (mounts != null)
            {
                string mountsAsString = ConvertListToString(mounts);
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacterMounts"
                                     };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("Mounts", mountsAsString);

                sqlCommand.ExecuteScalar();
                sqlConnection.Close();
            }
        }

        private void InsertCharacterEquipment(Items items, string characterId, string connectionString)
        {
            if (items != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand();
                if (items.Back != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Back.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Back.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Back.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Back.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Back))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Back.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Back.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Back.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Back.TooltipParams.Gems.Count >= 1 ? items.Back.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Back.TooltipParams.Gems.Count >= 2 ? items.Back.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Back.TooltipParams.Gems.Count >= 3 ? items.Back.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Back.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Back.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Back.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Back.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Chest != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Chest.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Chest.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Chest.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Chest.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Chest))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Chest.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Chest.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Chest.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Chest.TooltipParams.Gems.Count >= 1 ? items.Chest.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Chest.TooltipParams.Gems.Count >= 2 ? items.Chest.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Chest.TooltipParams.Gems.Count >= 3 ? items.Chest.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Chest.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Chest.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Chest.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Chest.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Feet != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Feet.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Feet.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Feet.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Feet.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Feet))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Feet.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Feet.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Feet.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Feet.TooltipParams.Gems.Count >= 1 ? items.Feet.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Feet.TooltipParams.Gems.Count >= 2 ? items.Feet.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Feet.TooltipParams.Gems.Count >= 3 ? items.Feet.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Feet.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Feet.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Feet.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Feet.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Finger1 != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Finger1.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Finger1.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Finger1.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Finger1.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Finger1))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Finger1.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Finger1.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Finger1.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Finger1.TooltipParams.Gems.Count >= 1 ? items.Finger1.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Finger1.TooltipParams.Gems.Count >= 2 ? items.Finger1.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Finger1.TooltipParams.Gems.Count >= 3 ? items.Finger1.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Finger1.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Finger1.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Finger1.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Finger1.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Finger2 != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Finger2.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Finger2.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Finger2.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Finger2.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Finger2))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Finger2.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Finger2.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Finger2.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Finger2.TooltipParams.Gems.Count >= 1 ? items.Finger2.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Finger2.TooltipParams.Gems.Count >= 2 ? items.Finger2.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Finger2.TooltipParams.Gems.Count >= 3 ? items.Finger2.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Finger2.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Finger2.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Finger2.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Finger2.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Hands != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Hands.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Hands.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Hands.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Hands.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Hands))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Hands.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Hands.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Hands.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Hands.TooltipParams.Gems.Count >= 1 ? items.Hands.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Hands.TooltipParams.Gems.Count >= 2 ? items.Hands.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Hands.TooltipParams.Gems.Count >= 3 ? items.Hands.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Hands.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Hands.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Hands.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Hands.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Head != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Head.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Head.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Head.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Head.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Head))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Head.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Head.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Head.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Head.TooltipParams.Gems.Count >= 1 ? items.Head.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Head.TooltipParams.Gems.Count >= 2 ? items.Head.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Head.TooltipParams.Gems.Count >= 3 ? items.Head.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Head.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Head.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Head.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Head.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Legs != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Legs.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Legs.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Legs.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Legs.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Legs))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Legs.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Legs.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Legs.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Legs.TooltipParams.Gems.Count >= 1 ? items.Legs.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Legs.TooltipParams.Gems.Count >= 2 ? items.Legs.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Legs.TooltipParams.Gems.Count >= 3 ? items.Legs.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Legs.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Legs.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Legs.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Legs.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.MainHand != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.MainHand.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.MainHand.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.MainHand.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.MainHand.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.MainHand))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.MainHand.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.MainHand.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.MainHand.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.MainHand.TooltipParams.Gems.Count >= 1 ? items.MainHand.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.MainHand.TooltipParams.Gems.Count >= 2 ? items.MainHand.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.MainHand.TooltipParams.Gems.Count >= 3 ? items.MainHand.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.MainHand.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.MainHand.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.MainHand.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.MainHand.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Neck != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Neck.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Neck.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Neck.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Neck.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Neck))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Neck.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Neck.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Neck.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Neck.TooltipParams.Gems.Count >= 1 ? items.Neck.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Neck.TooltipParams.Gems.Count >= 2 ? items.Neck.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Neck.TooltipParams.Gems.Count >= 3 ? items.Neck.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Neck.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Neck.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Neck.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Neck.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.OffHand != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.OffHand.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.OffHand.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.OffHand.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.OffHand.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.OffHand))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.OffHand.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.OffHand.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.OffHand.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.OffHand.TooltipParams.Gems.Count >= 1 ? items.OffHand.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.OffHand.TooltipParams.Gems.Count >= 2 ? items.OffHand.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.OffHand.TooltipParams.Gems.Count >= 3 ? items.OffHand.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.OffHand.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.OffHand.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.OffHand.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.OffHand.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Ranged != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Ranged.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Ranged.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Ranged.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Ranged.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Ranged))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Ranged.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Ranged.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Ranged.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Ranged.TooltipParams.Gems.Count >= 1 ? items.Ranged.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Ranged.TooltipParams.Gems.Count >= 2 ? items.Ranged.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Ranged.TooltipParams.Gems.Count >= 3 ? items.Ranged.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Ranged.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Ranged.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Ranged.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Ranged.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Shirt != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Shirt.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Shirt.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Shirt.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Shirt.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Shirt))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Shirt.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Shirt.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Shirt.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Shirt.TooltipParams.Gems.Count >= 1 ? items.Shirt.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Shirt.TooltipParams.Gems.Count >= 2 ? items.Shirt.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Shirt.TooltipParams.Gems.Count >= 3 ? items.Shirt.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Shirt.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Shirt.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Shirt.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Shirt.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Shoulder != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Shoulder.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Shoulder.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Shoulder.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Shoulder.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Shoulder))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Shoulder.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Shoulder.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Shoulder.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Shoulder.TooltipParams.Gems.Count >= 1 ? items.Shoulder.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Shoulder.TooltipParams.Gems.Count >= 2 ? items.Shoulder.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Shoulder.TooltipParams.Gems.Count >= 3 ? items.Shoulder.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Shoulder.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Shoulder.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Shoulder.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Shoulder.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Tabard != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Tabard.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Tabard.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Tabard.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Tabard.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Tabard))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Tabard.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Tabard.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Tabard.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Tabard.TooltipParams.Gems.Count >= 1 ? items.Tabard.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Tabard.TooltipParams.Gems.Count >= 2 ? items.Tabard.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Tabard.TooltipParams.Gems.Count >= 3 ? items.Tabard.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Tabard.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Tabard.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Tabard.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Tabard.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Trinket1 != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Trinket1.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Trinket1.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Trinket1.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Trinket1.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Trinket1))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Trinket1.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Trinket1.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Trinket1.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Trinket1.TooltipParams.Gems.Count >= 1 ? items.Trinket1.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Trinket1.TooltipParams.Gems.Count >= 2 ? items.Trinket1.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Trinket1.TooltipParams.Gems.Count >= 3 ? items.Trinket1.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Trinket1.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Trinket1.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Trinket1.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Trinket1.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Trinket2 != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Trinket2.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Trinket2.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Trinket2.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Trinket2.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Trinket2))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Trinket2.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Trinket2.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Trinket2.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Trinket2.TooltipParams.Gems.Count >= 1 ? items.Trinket2.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Trinket2.TooltipParams.Gems.Count >= 2 ? items.Trinket2.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Trinket2.TooltipParams.Gems.Count >= 3 ? items.Trinket2.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Trinket2.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Trinket2.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Trinket2.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Trinket2.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Waist != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Waist.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Waist.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Waist.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Waist.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Waist))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Waist.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Waist.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Waist.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Waist.TooltipParams.Gems.Count >= 1 ? items.Waist.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Waist.TooltipParams.Gems.Count >= 2 ? items.Waist.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Waist.TooltipParams.Gems.Count >= 3 ? items.Waist.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Waist.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Waist.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Waist.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Waist.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                if (items.Wrist != null)
                {
                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertItem"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("ItemId", items.Wrist.ID);
                    sqlCommand.Parameters.AddWithValue("Icon", items.Wrist.Icon);
                    sqlCommand.Parameters.AddWithValue("Name", items.Wrist.Name);
                    sqlCommand.Parameters.AddWithValue("Quality", items.Wrist.Quality);
                    sqlCommand.ExecuteScalar();

                    if (GotItemEnhancement(items.Wrist))
                    {
                        sqlCommand = new SqlCommand
                                         {
                                             CommandType = CommandType.StoredProcedure,
                                             Connection = sqlConnection,
                                             CommandText = "InsertCharacter_ItemEnhancement"
                                         };
                        sqlCommand.Parameters.Clear();
                        sqlCommand.Parameters.AddWithValue("ItemId", items.Wrist.ID);
                        sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                        sqlCommand.Parameters.AddWithValue("Enchant", items.Wrist.TooltipParams.Enchant);
                        sqlCommand.Parameters.AddWithValue("ExtraSocket", items.Wrist.TooltipParams.ExtraSocket);
                        sqlCommand.Parameters.AddWithValue("Gem0", (items.Wrist.TooltipParams.Gems.Count >= 1 ? items.Wrist.TooltipParams.Gems[0] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem1", (items.Wrist.TooltipParams.Gems.Count >= 2 ? items.Wrist.TooltipParams.Gems[1] : 0));
                        sqlCommand.Parameters.AddWithValue("Gem2", (items.Wrist.TooltipParams.Gems.Count >= 3 ? items.Wrist.TooltipParams.Gems[2] : 0));
                        sqlCommand.Parameters.AddWithValue("Reforge", items.Wrist.TooltipParams.Reforge);
                        sqlCommand.Parameters.AddWithValue("Seed", items.Wrist.TooltipParams.Seed);
                        sqlCommand.Parameters.AddWithValue("Set", ConvertListToString(items.Wrist.TooltipParams.Set));
                        sqlCommand.Parameters.AddWithValue("Suffix", items.Wrist.TooltipParams.Suffix);
                        sqlCommand.ExecuteScalar();
                    }
                }

                sqlCommand = new SqlCommand
                                 {
                                     CommandType = CommandType.StoredProcedure,
                                     Connection = sqlConnection,
                                     CommandText = "InsertCharacterEquipment"
                                 };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("AverageItemLevel", items.AverageItemLevel);
                sqlCommand.Parameters.AddWithValue("AverageItemLevelEquipped", items.AverageItemLevelEquipped);
                sqlCommand.Parameters.AddWithValue("Back", (items.Back != null ? items.Back.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Chest", (items.Chest != null ? items.Chest.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Feet", (items.Feet != null ? items.Feet.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Finger1", (items.Finger1 != null ? items.Finger1.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Finger2", (items.Finger2 != null ? items.Finger2.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Hands", (items.Hands != null ? items.Hands.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Head", (items.Head != null ? items.Head.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Legs", (items.Legs != null ? items.Legs.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("MainHand", (items.MainHand != null ? items.MainHand.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Neck", (items.Neck != null ? items.Neck.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("OffHand", (items.OffHand != null ? items.OffHand.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Ranged", (items.Ranged != null ? items.Ranged.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Shirt", (items.Shirt != null ? items.Shirt.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Shoulder", (items.Shoulder != null ? items.Shoulder.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Tabard", (items.Tabard != null ? items.Tabard.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Trinket1", (items.Trinket1 != null ? items.Trinket1.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Trinket2", (items.Trinket2 != null ? items.Trinket2.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Waist", (items.Waist != null ? items.Waist.ID.ToString() : ""));
                sqlCommand.Parameters.AddWithValue("Wrist", (items.Wrist != null ? items.Wrist.ID.ToString() : ""));
                sqlCommand.ExecuteScalar();

                sqlConnection.Close();
            }
        }

        private void InsertCharacterCompanions(IEnumerable<int> companions, string characterId, string connectionString)
        {
            if (companions != null)
            {
                string companionsAsString = ConvertListToString(companions);
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacterCompanions"
                                     };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("Companions", companionsAsString);

                sqlCommand.ExecuteScalar();
                sqlConnection.Close();
            }
        }

        private void InsertCharacterAppearance(Appearance appearance, string characterId, string connectionString)
        {
            if (appearance != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacterAppearance"
                                     };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("FaceVariation", appearance.FaceVariation);
                sqlCommand.Parameters.AddWithValue("FeatureVariation", appearance.FeatureVariation);
                sqlCommand.Parameters.AddWithValue("HairColor", appearance.HairColor);
                sqlCommand.Parameters.AddWithValue("HairVariation", appearance.HairVariation);
                sqlCommand.Parameters.AddWithValue("ShowCloak", appearance.ShowCloak);
                sqlCommand.Parameters.AddWithValue("ShowHelm", appearance.ShowHelm);
                sqlCommand.Parameters.AddWithValue("SkinColor", appearance.SkinColor);

                sqlCommand.ExecuteScalar();
                sqlConnection.Close();
            }
        }

        private void InsertCharacterAchievements(Achievements achievements, string characterId, string connectionString)
        {
            if (achievements != null)
            {
                string completedAchievements;
                string completedTimeStamps;
                ConvertAchievementsToString(achievements, out completedAchievements, out completedTimeStamps);

                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();
                var sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacterAchievements"
                                     };
                sqlCommand.Parameters.Clear();
                sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                sqlCommand.Parameters.AddWithValue("AchievementsCompleted", completedAchievements);
                sqlCommand.Parameters.AddWithValue("AchievementsCompletedTimestamp", completedTimeStamps);

                sqlCommand.ExecuteScalar();
                sqlConnection.Close();
            }
        }

        private void InsertCharacterPets(IEnumerable<Pet> pets, string characterId, string connectionString)
        {
            if (pets != null)
            {
                var sqlConnection = new SqlConnection(connectionString);
                sqlConnection.Open();

                var sqlCommand = new SqlCommand
                                     {
                                         Connection = sqlConnection,
                                         CommandText = "DELETE FROM Character_Pet WHERE CharacterId='" + characterId + "'"
                                     };
                sqlCommand.ExecuteNonQuery();

                foreach (Pet pet in pets)
                {
                    string petId = pet.Creature.ToString();
                    string petName = pet.Name;
                    string talentBuild = (pet.Talents != null ? pet.Talents.Build : "");

                    sqlCommand = new SqlCommand
                                     {
                                         CommandType = CommandType.StoredProcedure,
                                         Connection = sqlConnection,
                                         CommandText = "InsertCharacterPets"
                                     };
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.AddWithValue("CharacterId", characterId);
                    sqlCommand.Parameters.AddWithValue("PetId", petId);
                    sqlCommand.Parameters.AddWithValue("TalentBuild", talentBuild);
                    sqlCommand.Parameters.AddWithValue("Name", petName);

                    sqlCommand.ExecuteScalar();
                }
                sqlConnection.Close();
            }
        }

        private void ConvertAchievementsToString(Achievements achievements, out string completedAchievements, out string completedTimeStamps)
        {
            completedAchievements = "";
            completedTimeStamps = "";

            foreach (Achievement achievement in achievements.Achievements)
            {
                completedAchievements += achievement.ID + ",";
                var sqlDateTime = new SqlDateTime(achievement.Timestamp);
                completedTimeStamps += sqlDateTime.ToString() + ",";
            }
        }

        private string ConvertListToString(IEnumerable<int> listToConvert)
        {
            string result = "";
            foreach (int i in listToConvert)
                result = result + (i + ",");
            return result;
        }

        private void SetRegion(string region)
        {
            switch (region)
            {
                case "EU":
                    WowAPIData.Region = Region.Europe;
                    break;
                case "US":
                    WowAPIData.Region = Region.NorthAmerica;
                    break;
            }
        }

        private bool GotItemEnhancement(Item item)
        {
            if (item.TooltipParams.Enchant == 0 && !item.TooltipParams.ExtraSocket && item.TooltipParams.Gems.Count == 0 && item.TooltipParams.Reforge == 0 && item.TooltipParams.Set.Count == 0)
            {
                return false;
            }

            return true;
        }
    }
}